'use strict';

const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class ActivityLog extends Model {
    static associate(models) {
      // 定义关联
      ActivityLog.belongsTo(models.User, { foreignKey: 'userId' });
    }
  }
  
  ActivityLog.init({
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'users',
        key: 'id'
      }
    },
    date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    activeDuration: {
      type: DataTypes.INTEGER,
      defaultValue: 0, // 以秒为单位记录活跃时长
      comment: '活跃时长（秒）'
    },
    sessionCount: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '会话数'
    },
    messagesSent: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '发送消息数'
    },
    messagesReceived: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '接收消息数'
    }
  }, {
    sequelize,
    modelName: 'ActivityLog',
    tableName: 'activity_logs',
    timestamps: true,
    indexes: [
      {
        name: 'activity_logs_user_id_date',
        fields: ['userId', 'date'],
        unique: true
      }
    ]
  });
  
  return ActivityLog;
}; 